<?php

namespace app\api\controller;

use app\api\service\ShoppingCartService;
use app\api\service\LoginService;
use app\BaseController;
use think\facade\Request;
use app\Response;

class ShoppingCart  extends BaseController
{
    public function dataList(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::post("page");
            $num = Request::post("num");
            $shi_type = Request::post("shi_type");
            $res = ShoppingCartService::dataListService($user_id,$page,$num,$shi_type);
            return $res;
        }
    }


    public function increase(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $p_id = Request::post("p_id");
            $num = Request::post("num",1);
            $p_name = Request::post("p_name",1);
            $res = ShoppingCartService::increaseDataService($user_id,$p_id,$p_name,$num);
            return $res;
        }
    }

    public function delCart(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            $res = ShoppingCartService::removeDataService($user_id,$id);
            return $res;
        }
    }


    public function editNum(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            $type = Request::post("type");// 1加 2减
            $num = Request::post("num",1);//操作数量
            $res = ShoppingCartService::editNumService($user_id,$id,$type,$num);
            return $res;
        }
    }
}